package palms.messages;

import java.io.IOException;
import java.net.Socket;

import javax.crypto.SealedObject;

import palms.exceptions.InvalidClassMessage;
import palms.exceptions.InvalidTicketMessage;
import palms.exceptions.InvalidUserMessage;
import palms.security.PalmsSecurityManager;

public class AuthResponse extends PMessage {

	private static final long serialVersionUID = 5751986561580267196L;

	private SealedObject tgsTicket;
	private SealedObject key;
	
	
	public AuthResponse(SealedObject tgsTicket, SealedObject key) {
		this.tgsTicket = tgsTicket;
		this.key = key;
	}
	public AuthResponse() {
	}
	public SealedObject getTgsTicket() {
		return tgsTicket;
	}
	public void setTgsTicket(SealedObject tgsTicket) {
		this.tgsTicket = tgsTicket;
	}
	public SealedObject getKey() {
		return key;
	}
	public void setKey(SealedObject key) {
		this.key = key;
	}
	
	public void sendTroughSocket(Socket s) throws IOException{
		PalmsSecurityManager.sendObjectTroughtSocket(s, new AuthResponse());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getTgsTicket());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getKey());
	}
	
	public static AuthResponse receiveTroughSocket(Socket s) throws IOException, ClassNotFoundException, InvalidClassMessage, InvalidUserMessage, InvalidTicketMessage{
		Object o = PalmsSecurityManager.receiveObjectTroughSocket(s);
		if(!(o instanceof AuthResponse))
			PalmsSecurityManager.throwCommException(o);
		else{
			AuthResponse ar = (AuthResponse)o;
			SealedObject ticket = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			SealedObject key = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			ar.setTgsTicket(ticket);
			ar.setKey(key);
			return ar;
		}
		return null;
	}
	
}
